<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author Cheng
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handleForm({})" v-auth="'supervisor:approval:process:supervisorApproval:edit'">
          <Icon icon="i-fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ tid: record.tid })">
          {{ record.mainProcessId }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <BpmRuntimeTrace @register="registerTraceModal" />
  </div>
</template>
<script lang="ts" setup name="ViewsSupervisorApprovalProcessSupervisorApprovalList">
  import { unref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { supervisorApprovalDelete, supervisorApprovalListData } from '/@/api/supervisor.approval.process/supervisorApproval';
  import { useDrawer } from '/@/components/Drawer';
  import { useModal } from '/@/components/Modal';
  import { BpmRuntimeTrace } from '/@/components/Bpm';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';

  const { t } = useI18n('supervisor.approval.process.supervisorApproval');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);

  const getTitle = {
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: meta.title || t('超时监督员审批流程管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { md: 8, lg: 6 },
    labelWidth: 90,
    schemas: [
      {
        label: t('主流程id  由那个流程发起的'),
        field: 'mainProcessId',
        component: 'Input',
      },
      {
        label: t('隐患类型'),
        field: 'hazardType',
        component: 'Input',
      },
      {
        label: t('企业ID'),
        field: 'enterpriseId',
        component: 'Input',
      },
      {
        label: t('企业名称'),
        field: 'enterpriseName',
        component: 'Input',
      },
      {
        label: t('建筑ID'),
        field: 'buildId',
        component: 'Input',
      },
      {
        label: t('建筑名称'),
        field: 'buildName',
        component: 'Input',
      },
      {
        label: t('区域ID'),
        field: 'regionId',
        component: 'Input',
      },
      {
        label: t('区域名称'),
        field: 'regionName',
        component: 'Input',
      },
      {
        label: t('隐患说明'),
        field: 'hazardDescription',
        component: 'Input',
      },
      {
        label: t('是否超时'),
        field: 'isTimeout',
        component: 'Input',
      },
      {
        label: t('创建时间'),
        field: 'createTime',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD HH:mm',
          showTime: { format: 'HH:mm' },
        },
      },
      {
        label: t('延期时间'),
        field: 'delayTime',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD HH:mm',
          showTime: { format: 'HH:mm' },
        },
      },
      {
        label: t('监督员姓名'),
        field: 'supervisorName',
        component: 'Input',
      },
      {
        label: t('监督员code'),
        field: 'supervisorCode',
        component: 'Input',
      },
      {
        label: t('流程状态  已完成  未完成'),
        field: 'processStatus',
        component: 'Input',
      },
      {
        label: t('将流程中的id保存'),
        field: 'redisTaskCache',
        component: 'Input',
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('主流程id  由那个流程发起的'),
      dataIndex: 'mainProcessId',
      key: 'a.main_process_id',
      sorter: true,
      width: 230,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('隐患类型'),
      dataIndex: 'hazardType',
      key: 'a.hazard_type',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('企业ID'),
      dataIndex: 'enterpriseId',
      key: 'a.enterprise_id',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('企业名称'),
      dataIndex: 'enterpriseName',
      key: 'a.enterprise_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('建筑ID'),
      dataIndex: 'buildId',
      key: 'a.build_id',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('建筑名称'),
      dataIndex: 'buildName',
      key: 'a.build_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('区域ID'),
      dataIndex: 'regionId',
      key: 'a.region_id',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('区域名称'),
      dataIndex: 'regionName',
      key: 'a.region_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('隐患说明'),
      dataIndex: 'hazardDescription',
      key: 'a.hazard_description',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('是否超时'),
      dataIndex: 'isTimeout',
      key: 'a.is_timeout',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('创建时间'),
      dataIndex: 'createTime',
      key: 'a.create_time',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('延期时间'),
      dataIndex: 'delayTime',
      key: 'a.delay_time',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('监督员姓名'),
      dataIndex: 'supervisorName',
      key: 'a.supervisor_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('监督员code'),
      dataIndex: 'supervisorCode',
      key: 'a.supervisor_code',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('流程状态  已完成  未完成'),
      dataIndex: 'processStatus',
      key: 'a.process_status',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('将流程中的id保存'),
      dataIndex: 'redisTaskCache',
      key: 'a.redis_task_cache',
      sorter: true,
      width: 130,
      align: 'left',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'i-clarity:note-edit-line',
        title: t('编辑超时监督员审批流程'),
        onClick: handleForm.bind(this, { tid: record.tid }),
        auth: 'supervisor:approval:process:supervisorApproval:edit',
      },
      {
        icon: 'i-ant-design:delete-outlined',
        color: 'error',
        title: t('删除超时监督员审批流程'),
        popConfirm: {
          title: t('是否确认删除超时监督员审批流程'),
          confirm: handleDelete.bind(this, record),
        },
        auth: 'supervisor:approval:process:supervisorApproval:edit',
        ifShow: () => record.status == '9',
      },
      {
        icon: 'i-fluent:flowchart-20-regular',
        title: t('流程追踪'),
        onClick: handleTrace.bind(this, record),
        ifShow: () => record.status != '9',
      },
    ],
  };

  const [registerTable, { reload }] = useTable({
    api: supervisorApprovalListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
  });

  const [registerDrawer, { openDrawer }] = useDrawer();

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const params = { tid: record.tid };
    const res = await supervisorApprovalDelete(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  function handleSuccess(record: Recordable) {
    reload({ record });
  }

  const [registerTraceModal, { openModal: traceModel }] = useModal();

  function handleTrace(record: Recordable) {
    traceModel(true, { formKey: 'supervisor_approval_process', bizKey: record.id });
  }
</script>
